package com.piece.file.service;

import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.file.model.TbFileClient;
import com.piece.file.repository.TbFileClientRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Service
@Transactional
public class TbFileClientService extends BaseService<TbFileClient, String> {

    @Resource
    private TbFileClientRepository tbFileClientRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(tbFileClientRepository);
    }

    /**
     * 校验客户端类别是否唯一
     *
     * @param clientId  数据源标识
     * @param clientType 数据源名称
     * @return true: 通过 false: 重复
     */
    public boolean checkUnique(String clientId, String clientType) {
        String hql = "from TbFileClient where clientType = ?0";
        Object[] params = new Object[]{clientType};
        if (StringUtil.isNotEmpty(clientId)) {
            hql = "from TbFileClient where clientId != ?0 and clientType = ?1";
            params = new Object[]{clientId, clientType};
        }
        long count = tbFileClientRepository.count(hql, params, true);
        return count <= 0;
    }
}
